<?php

class Offer
{
	public function __construct($method)
	{
		require_once('rb.php');
		R::setup('mysql:host=localhost;dbname=teamNut', 'root', '');

		if ($method == 'GET')
		{
			if (isset($_GET['id']))
			{
				$this->getById($_GET['id']);
			}
			else if (isset($_GET['all']))
			{
				$this->getAll();
			}
			else
			{
				$this->deleteById($_GET['delete']);
			}
		}
		else
		{
			$method = isset($_POST['id']) ? 'update' : 'put';
			$_POST['id'] = isset($_POST['id']) ? $_POST['id'] : '0';
			$this->$method($_POST['type'],
					$_POST['title'],
					$_POST['startDate'],
					$_POST['endDate'],
					$_POST['price'],
					$_POST['thumbnail'],
					$_POST['image'],
					$_POST['description'],
					$_POST['transportType'],
					$_POST['itinerary'],
					$_POST['waterbody'],
					$_POST['active'],
					$_POST['id']
				);
		}
	}

	private function deleteById($id)
	{
		$offer = R::load('post', $id);
    	R::trash($offer);
	}

	private function getResultArray($offer)
	{
		$result = array(
			'id' => $offer->id,
			'type' => $offer->type,
			'title' => $offer->title,
			'startDate' => $offer->startDate,
			'endDate' => $offer->endDate,
			'price' => $offer->price,
			'thumbnailUrl' => $offer->thumbnail,
			'imageUrl' => $offer->image,
			'description' => $offer->descr,
			'transportType' => $offer->transportType,
			'itinerary' => $offer->itinerary,
			'waterbody' => $offer->waterbody,
			'active' => $offer->active == 'on' ? true : false
		);

		return $result;
	}

	public function getById($id)
	{
		$offer = R::load('offer', $id);
		$result = $this->getResultArray($offer);
		echo json_encode($result);
	}

	public function put($type, $title, $startDate, $endDate, $price,
	 $thumbnail, $image, $descr, $transportType, $itinerary, $waterbody, $active, $id)
	{
		$offer = R::dispense('offer');
		$offer->type = $type;
		$offer->title = $title;
		$offer->startDate = $startDate;
		$offer->endDate = $endDate;
		$offer->price = $price;
		$offer->thumbnail = $thumbnail;
		$offer->image = $image;
		$offer->descr = $descr;
		$offer->transportType = $transportType;
		$offer->itinerary = $itinerary;
		$offer->waterbody = $waterbody;
		$offer->active = $active;
		R::store($offer);
	}

	public function update($type, $title, $startDate, $endDate, $price,
	 $thumbnail, $image, $descr, $transportType, $itinerary, $waterbody, $active, $id)
	{
		$offer = R::load('offer', $id);
		$offer->type = $type;
		$offer->title = $title;
		$offer->startDate = $startDate;
		$offer->endDate = $endDate;
		$offer->price = $price;
		$offer->thumbnail = $thumbnail;
		$offer->image = $image;
		$offer->descr = $descr;
		$offer->transportType = $transportType;
		$offer->itinerary = $itinerary;
		$offer->waterbody = $waterbody;
		$offer->active = $active;
		R::store($offer);
	}

	public function getAll()
	{
		$offers = R::findAll('offer');
		$result = array();
		foreach ($offers as $key => $value) {
			array_push($result, $this->getResultArray($value));
		}
		echo 'var offers = '.json_encode($result).';';
	}
}

new Offer($_SERVER["REQUEST_METHOD"]);